package com.live.shop.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class KeywordModel  implements Serializable {
    int keywordId;//关键词ID
    String question;//问题
    String keyword;//关键词
    int useNum;//使用次数
    int keywordStatus;//启用状态 1未启用 2启用
    String createTime;
    List<KeywordRecordModel> listRecord;

    public static JSONObject model2Json(KeywordModel model){
        JSONObject json = new JSONObject();
        try {
            json.put("keywordId",model.getKeywordId());
            json.put("question",model.getQuestion());
            json.put("keyword",model.getKeyword());
            json.put("keywordStatus",model.getKeywordStatus());
            json.put("useNum",model.getUseNum());
            json.put("createTime",model.getCreateTime());
            JSONArray recordArray = new JSONArray();
            for (KeywordRecordModel m:model.getListRecord()) {
                recordArray.put(m.model2Json(m));
            }
            json.put("listRecord",recordArray);

        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    public static KeywordModel Json2model(JSONObject json){
        KeywordModel model = new KeywordModel();
        try {
            model.setKeywordId(json.getInt("keywordId"));
            model.setQuestion(json.getString("question"));
            model.setKeyword(json.getString("keyword"));
            model.setKeywordStatus(json.getInt("keywordStatus"));
            model.setUseNum(json.getInt("useNum"));
            model.setCreateTime(json.getString("createTime"));

            List<KeywordRecordModel> list = new ArrayList<>();

            JSONArray recordArray = json.getJSONArray("listRecord");
            for (int i =0 ; i < recordArray.length();i++) {
                KeywordRecordModel reModel = new KeywordRecordModel();
                JSONObject recordJson = recordArray.getJSONObject(i);

                reModel.setId(recordJson.getInt("id"));
                reModel.setKeywordId(recordJson.getInt("keywordId"));
                reModel.setAnswerTxt(recordJson.getString("answerTxt"));
                reModel.setAnswerType(recordJson.getInt("answerType"));
                reModel.setAnswerState(recordJson.getInt("answerState"));
                reModel.setAnswerAudio(recordJson.getString("answerAudio"));
                reModel.setUseNum(recordJson.getInt("useNum"));
                reModel.setCreateTime(recordJson.getString("createTime"));
                list.add(reModel);
            }
            model.setListRecord(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return model;
    }

    public int getKeywordStatus() {
        return keywordStatus;
    }

    public void setKeywordStatus(int keywordStatus) {
        this.keywordStatus = keywordStatus;
    }

    public int getKeywordId() {
        return keywordId;
    }

    public void setKeywordId(int keywordId) {
        this.keywordId = keywordId;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public int getUseNum() {
        return useNum;
    }

    public void setUseNum(int useNum) {
        this.useNum = useNum;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public List<KeywordRecordModel> getListRecord() {
        return listRecord;
    }

    public void setListRecord(List<KeywordRecordModel> listRecord) {
        this.listRecord = listRecord;
    }
}
